// AppMenu class : Application menu

package appForm;

import java.awt.*;
import java.awt.event.*;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.KeyStroke;
import appPref.Para;

public class AppMenu extends JMenuBar implements ActionListener {

  private AppFrame appFrame;
  private ButtonGroup group = new ButtonGroup();
  private Para para = Para.getPara();

  private static String file;
  private static String exit;
  private static String edit;
  private static String selectPictures;
  private static String deselectPictures;
  private static String deleteSelectedPictures;
  private static String view;
  private static String hideSelectedPictures;
  private static String showPictures;
  private static String increaseSelection;
  private static String decreaseSelection;
  private static String changeBackGround;
  private static String backGround1;
  private static String backGround2;
  private static String backGround3;
  private static String help;
  private static String about;

	private static final String ICON1 = "image/backGround1Icon.gif";
	private static final String ICON2 = "image/backGround2Icon.gif";
	private static final String ICON3 = "image/backGround3Icon.gif";

  {
    file =                            para.getT("file");
    exit =	                          para.getT("exit");
    edit =	                          para.getT("edit");
    selectPictures =	                para.getT("selectPictures");
    deselectPictures =	              para.getT("deselectPictures");
    deleteSelectedPictures =	        para.getT("deleteSelectedPictures");
    view =                            para.getT("view");
    hideSelectedPictures =	          para.getT("hideSelectedPictures");
    showPictures =	                  para.getT("showPictures");
    increaseSelection =	              para.getT("increaseSelection");
    decreaseSelection =	              para.getT("decreaseSelection");
    changeBackGround =                para.getT("changeBackGround");
    backGround1 =                     para.getT("backGround1");
    backGround2 =                     para.getT("backGround2");
    backGround3 =                     para.getT("backGround3");
    help =	                          para.getT("help");
    about =	                          para.getT("about");
  }

	private static final int EXIT			   = 11;
	private static final int FRENCH  	   = 12;
	private static final int ENGLISH 	   = 13;
	private static final int SELECTP	   = 21;
	private static final int DESELECTP   = 22;
  private static final int DELETEP	   = 23;
  private static final int HIDEP  	   = 31;
	private static final int SHOWP       = 32;
  private static final int INCREASE	   = 33;
	private static final int DECREASE	   = 34;
	private static final int BACKGROUND1 = 35;
	private static final int BACKGROUND2 = 36;
	private static final int BACKGROUND3 = 37;
  private static final int ABOUT		   = 42;

  private	JMenu			menuFile				 = new JMenu(file);
  private JMenuItem menuFileExit		 = new JMenuItem(exit);
  private JMenu 		menuEdit 				 = new JMenu(edit);
  private JMenuItem menuEditSelectp	 = new JMenuItem(selectPictures);
  private JMenuItem menuEditDeselectp= new JMenuItem(deselectPictures);
  private JMenuItem menuEditDeletep  = new JMenuItem(deleteSelectedPictures);
  private	JMenu 		menuView				 = new JMenu(view);
  private JMenuItem menuViewHidep		 = new JMenuItem(hideSelectedPictures);
  private JMenuItem menuViewShowp		 = new JMenuItem(showPictures);
  private JMenuItem menuViewIncrease = new JMenuItem(increaseSelection);
  private JMenuItem menuViewDecrease = new JMenuItem(decreaseSelection);
  private	JMenu 		menuChange    	 = new JMenu(changeBackGround);
  private	JMenuItem	menuChange1   	 = new JRadioButtonMenuItem(backGround1, para.getImageIcon(ICON1));
  private	JMenuItem	menuChange2   	 = new JRadioButtonMenuItem(backGround2, para.getImageIcon(ICON2));
  private	JMenuItem	menuChange3   	 = new JRadioButtonMenuItem(backGround3, para.getImageIcon(ICON3));
  private	JMenu 		menuHelp				 = new JMenu(help);
  private JMenuItem menuHelpAbout 	 = new JMenuItem(about);

  public AppMenu() {
  	super();

    menuFileExit.setActionCommand("Exit");
    menuFileExit.addActionListener(this);
    menuFileExit.setMnemonic(KeyEvent.VK_E);
    menuFileExit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, ActionEvent.ALT_MASK));

    menuEditSelectp.setActionCommand("Selectp");
    menuEditSelectp.addActionListener(this);
    menuEditSelectp.setMnemonic(KeyEvent.VK_S);
    menuEditSelectp.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, ActionEvent.CTRL_MASK));

    menuEditDeselectp.setActionCommand("Deselectp");
    menuEditDeselectp.addActionListener(this);
    menuEditDeselectp.setMnemonic(KeyEvent.VK_D);
    menuEditDeselectp.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_D, ActionEvent.CTRL_MASK));

    menuEditDeletep.setActionCommand("Deletep");
    menuEditDeletep.addActionListener(this);
    menuEditDeletep.setMnemonic(KeyEvent.VK_E);
    menuEditDeletep.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, ActionEvent.CTRL_MASK));

    menuViewHidep.setActionCommand("Hidep");
    menuViewHidep.addActionListener(this);
    menuViewHidep.setMnemonic(KeyEvent.VK_E);
    menuViewHidep.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_H, ActionEvent.CTRL_MASK));

    menuViewShowp.setActionCommand("Showp");
    menuViewShowp.addActionListener(this);
    menuViewShowp.setMnemonic(KeyEvent.VK_S);
    menuViewShowp.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.CTRL_MASK));

    menuViewIncrease.setActionCommand("Increase");
    menuViewIncrease.addActionListener(this);
    menuViewIncrease.setMnemonic(KeyEvent.VK_I);
    menuViewIncrease.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_ADD, ActionEvent.CTRL_MASK));

    menuViewDecrease.setActionCommand("Decrease");
    menuViewDecrease.addActionListener(this);
    menuViewDecrease.setMnemonic(KeyEvent.VK_D);
    menuViewDecrease.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_SUBTRACT, ActionEvent.CTRL_MASK));

    menuChange1.setActionCommand("BackGround1");
    menuChange1.addActionListener(this);
    menuChange1.setMnemonic(KeyEvent.VK_O);
    menuChange1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_1, ActionEvent.CTRL_MASK));
    menuChange1.setSelected(true);
    group.add(menuChange1);

    menuChange2.setActionCommand("BackGround2");
    menuChange2.addActionListener(this);
    menuChange2.setMnemonic(KeyEvent.VK_M);
    menuChange2.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_2, ActionEvent.CTRL_MASK));
    group.add(menuChange2);

    menuChange3.setActionCommand("BackGround3");
    menuChange3.addActionListener(this);
    menuChange3.setMnemonic(KeyEvent.VK_T);
    menuChange3.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_3, ActionEvent.CTRL_MASK));
    group.add(menuChange3);

    menuHelpAbout.setActionCommand("About");
    menuHelpAbout.addActionListener(this);
    menuHelpAbout.setMnemonic(KeyEvent.VK_O);

    menuFile.add(menuFileExit);

    menuEdit.add(menuEditSelectp);
    menuEdit.add(menuEditDeselectp);
    menuEdit.addSeparator();
    menuEdit.add(menuEditDeletep);

    menuView.add(menuViewHidep);
    menuView.add(menuViewShowp);
    menuView.addSeparator();
    menuView.add(menuViewIncrease);
    menuView.add(menuViewDecrease);
    menuView.addSeparator();
    menuView.add(menuChange);
    menuChange.setMnemonic('C');
    menuChange.add(menuChange1);
    menuChange.add(menuChange2);
    menuChange.add(menuChange3);

    menuHelp.add(menuHelpAbout);

    this.add(menuFile);
    menuFile.setMnemonic('F');
    this.add(menuEdit);
    menuEdit.setMnemonic('D');
    this.add(menuView);
    menuView.setMnemonic('I');
    this.add(menuHelp);
    menuHelp.setMnemonic('E');
	}

  public AppMenu(AppFrame aFrame) {
    this();
    this.setAppFrame(aFrame);
  }

  public AppFrame getAppFrame() {
  	return appFrame;
  }

  public void setAppFrame(AppFrame aFrame) {
  	appFrame = aFrame;
  }

  private void displayAbout() {
    AppAbout about = new AppAbout(appFrame);
    Dimension aboutSize = about.getPreferredSize();
    Dimension frameSize = appFrame.getSize();
    Point frameLocation = appFrame.getLocation();
    int x = ((frameSize.width - aboutSize.width) / 2) + frameLocation.x;
    int y = ((frameSize.height - aboutSize.height) / 2) + frameLocation.y;
    about.setLocation(x, y);
    about.setModal(true);
    about.setVisible(true);
  }

  private int getCommand(String aCommand) {
    int	action = -1;
    if      (aCommand.equals("Exit"))        action = EXIT;
    else if (aCommand.equals("Selectp"))     action = SELECTP;
    else if (aCommand.equals("Deselectp"))   action = DESELECTP;
    else if (aCommand.equals("Deletep"))     action = DELETEP;
    else if (aCommand.equals("Hidep"))       action = HIDEP;
    else if (aCommand.equals("Showp"))       action = SHOWP;
    else if (aCommand.equals("Increase"))    action = INCREASE;
    else if (aCommand.equals("Decrease"))    action = DECREASE;
    else if (aCommand.equals("BackGround1")) action = BACKGROUND1;
    else if (aCommand.equals("BackGround2")) action = BACKGROUND2;
    else if (aCommand.equals("BackGround3")) action = BACKGROUND3;
    else if (aCommand.equals("About"))       action = ABOUT;
    return action;
  }

  public void actionPerformed(ActionEvent e) {
		String command = e.getActionCommand();
    switch (this.getCommand(command)) {
    	case EXIT:        appFrame.exit();
                        break;
      case SELECTP:     appFrame.getDiagram().selectAll();
                        break;
    	case DESELECTP:   appFrame.getDiagram().deSelectAll();
                        break;
      case DELETEP:     appFrame.getDiagram().deleteSelection();
                        break;
      case HIDEP:       appFrame.getDiagram().hideSelection();
                        break;
    	case SHOWP:       appFrame.getDiagram().showPictures();
                        break;
      case INCREASE:    appFrame.getDiagram().increaseSelection();
                        break;
    	case DECREASE:    appFrame.getDiagram().decreaseSelection();
                        break;
    	case BACKGROUND1: appFrame.getDiagram().switchBackGround1();
                        break;
    	case BACKGROUND2: appFrame.getDiagram().switchBackGround2();
                        break;
    	case BACKGROUND3: appFrame.getDiagram().switchBackGround3();
                        break;
      case ABOUT:       this.displayAbout();
                        break;
    }
  }
}
